package cn.edu.leetcode.动态规划;

public class 解码方法 {
    public static void main(String[] args) {
        System.out.println(numDecodings("1123"));
        System.out.println(numDecodings("1201234"));
        System.out.println(numDecodings("2101"));
        System.out.println(numDecodings("21"));
        System.out.println(numDecodings("10"));
    }

    public static int numDecodings(String s) {
        if (s.charAt(0) == '0') return 0;

        int[] nums = new int[s.length()];
        nums[0] = 1;

        for (int i = 1; i < s.length(); i++) {
            Integer integer = Integer.valueOf(s.substring(i - 1, i + 1));
            if (integer >= 10 && integer <= 26) {
                if (integer % 10 == 0) {
                    if (i - 2 < 0) {
                        nums[i] = 1;
                    } else {
                        nums[i] = nums[i - 2];
                    }
                } else {
                    if (i - 2 < 0) {
                        nums[i] = nums[i - 1] + 1;
                    } else {
                        nums[i] = nums[i - 1] + nums[i - 2];
                    }
                }
            } else if (integer % 10 == 0) {
                return 0;
            } else {
                nums[i] = nums[i - 1];
            }
        }
        return nums[s.length() - 1];
    }
}
